package cn.kgc.springboot.service.impl;

import cn.kgc.springboot.common.CodeMsg;
import cn.kgc.springboot.entity.RolePer;
import cn.kgc.springboot.entity.UserRole;
import cn.kgc.springboot.mapper.RolePerMapper;
import cn.kgc.springboot.mapper.UserRoleMapper;
import cn.kgc.springboot.util.LayTable;
import cn.kgc.springboot.vo.RoleSelect;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springboot.entity.Role;
import cn.kgc.springboot.service.RoleService;
import cn.kgc.springboot.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private  RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper  userRoleMapper;

    @Autowired
    private RolePerMapper rolePerMapper;

    @Override
    public List<RoleSelect> getRoleSelect() {

        List<Role> roles = roleMapper.selectList(null);
        ArrayList<RoleSelect> roleSelects = new ArrayList<>();
        //遍历集合
        for (Role role : roles) {
            RoleSelect roleSelect = new RoleSelect().setName(role.getName()).setValue(role.getId());
            roleSelects.add(roleSelect);
        }
        return roleSelects;
    }

    @Override
    public LayTable selectByPage(String name, Integer page, Integer limit) {

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();

        roleQueryWrapper.eq(name!=null&&!name.equals(""), "name", name);

        Page<Role> rolePage = new Page<>(page, limit);
        roleMapper.selectPage(rolePage, roleQueryWrapper);

        //获取分页数据  总条数

        List<Role> records = rolePage.getRecords();
        long total = rolePage.getTotal();

        return new LayTable().setMsg(CodeMsg.LAY_SUCCESS.getMsg())
                            .setCode(CodeMsg.LAY_SUCCESS.getCode())
                            .setCount(total)
                            .setData(records);
    }

    // 删除角色信息     角色   用户角色关系表   角色权限关系表
    @Override
    public LayTable deleteRole(Integer[] ids) {

        // 1. 判定删除的角色 是否有用户在占用  有则禁止删除  没有则删除
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.in("role_id", ids);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);

        if(ObjectUtils.isNotNull(userRoles)){
            return  new LayTable().setMsg(CodeMsg.USER_ROLE_EXIS.getMsg()).setCode(CodeMsg.USER_ROLE_EXIS.getCode());
        }


        // 2.删除角色信息
        roleMapper.deleteBatchIds(Arrays.asList(ids));

        // 3. 删除角色权限关系表中的数据
        for (Integer id : ids) {
            QueryWrapper<RolePer> rolePerQueryWrapper = new QueryWrapper<>();
            rolePerQueryWrapper.eq("role_id",id);
            rolePerMapper.delete(rolePerQueryWrapper);
        }

        return new LayTable().setMsg(CodeMsg.SUCCESS.getMsg()).setCode(CodeMsg.SUCCESS.getCode());
    }

    @Override
    public LayTable updateRoleAuth(Integer roleId, Integer[] authIds) {

        // 删除当前用户对应的权限信息   roleId
        QueryWrapper<RolePer> rolePerQueryWrapper = new QueryWrapper<>();
        rolePerQueryWrapper.eq("role_id", roleId);
        rolePerMapper.delete(rolePerQueryWrapper);

        // 添加当前用户最新的权限信息
        for (Integer authId : authIds) {
            RolePer rolePer = new RolePer();
            rolePer.setRoleId(roleId);
            rolePer.setPerId(authId);
            rolePerMapper.insert(rolePer);
        }

        return new LayTable().setCode(CodeMsg.SUCCESS.getCode()).setMsg(CodeMsg.SUCCESS.getMsg());
    }
}




